// pages/feedback/feedback.js
import Toast from '@vant/weapp/toast/toast';
import * as utils from '../../utils/util.js';
import upload from '../../utils/upload.js';
Page({

  /**
   * 页面的初始数据
   */
  data: {
    isSave:false,
    t_length: '800',
    maxLength: 800,
    minLength: 5,
    description: '',
    contactInfo: '',
    errorMessage: '',
    currentIndex:1,
    type:'功能异常',
    flag:false,
    picJson:[],
    subAgain:true,
    imgcount:3
  },
  changeType(e){
    let types = ['功能异常','产品建议','其他'];
    let index = e.currentTarget.dataset.index
    this.setData({
      currentIndex: index,
      type: types[index-1]
    })
  },
  changeImgs(){
    let that = this;
    //上传图片
    wx.chooseImage({
      count: that.data.imgcount,
      sizeType: ['original', 'compressed'],
      sourceType: ['album', 'camera'],
      success(res) {
        const tempFilePaths = res.tempFilePaths;
        let len = tempFilePaths.length;
        let imslen = that.data.picJson.length;
        if ((imslen + len)<=3){
          that.setData({
            picJson: that.data.picJson.concat(tempFilePaths),
            imgcount: (3 - len)
          })
        }else{
          wx.showToast({
            title: '最多上传3张图片',
            icon:'none'
          })
        }
        
      }
    })
  },
  bindText(e) {
    let description = e.detail.value;
    let arlength = parseInt(description.length); //获取已经输入的总字数
    let reg = /([^\u0020-\u007E\u00A0-\u00BE\u2E80-\uA4CF\uF900-\uFAFF\uFE30-\uFE4F\uFF00-\uFFEF\u0080-\u009F\u2000-\u201f\u2026\u2022\u20ac\r\n])|(\s)/g;
    if (reg.test(description)){
      wx.showToast({
        title: '问题描述只能输入汉字、英文、数字、符号，请您检查输入的内容哦',
        icon: 'none'
      })
      this.setData({ flag: true })
    }else{
      this.setData({ description: e.detail.value })
    }
    if(arlength <= this.data.maxLength){
      this.setData ({
        t_length: this.data.maxLength - arlength
      })
    }
  },
  bindblur(e) {
    let reg = /([^\u0020-\u007E\u00A0-\u00BE\u2E80-\uA4CF\uF900-\uFAFF\uFE30-\uFE4F\uFF00-\uFFEF\u0080-\u009F\u2000-\u201f\u2026\u2022\u20ac\r\n])|(\s)/g;
    let description = e.detail.value;
    if(description.length<5 || description.length>800){
      Toast('问题描述长度范围为5-800字，请您重新输入')
      this.setData({
        flag: true,
      })
    } else if (reg.test(description)){
      // Toast('描述只能输入汉字、英文、数字、符号，请您检查输入的内容哦')
      this.setData({
        flag: true
      })
    }else{
      this.setData({
        description: e.detail.value,
        flag: false
      })
    }
  },
  //删除图片
  deleteImg(e){
    console.log(e)
    let imgs = this.data.picJson;
    let itemIndex = e.currentTarget.dataset.currentindex;
    imgs.splice(itemIndex, 1);
    this.setData({
      picJson: imgs,
      imgcount: (3 - this.data.picJson.length)
    })
    console.log(this.data.picJson)
  },
  bindphone(e) {
    let contactInfo = e.detail.value;
    console.log(utils.checkNum(contactInfo))
    if (contactInfo != ''){
      if (!utils.checkNum(contactInfo)) {
        if (!utils.isEmail(contactInfo)) {
          this.setData({
            errorMessage: '请输入正确的手机号或邮箱',
            contactInfo: '',
            flag: true
          })
        } else {
          this.setData({
            errorMessage: '',
            contactInfo: e.detail.value,
            flag: false
          })
        }
      } else {
        if (contactInfo.length != 11) {
          this.setData({
            errorMessage: '请输入正确的手机号或邮箱',
            flag: true
          })
        } else if (!utils.checkPhone(contactInfo)) {
          console.log(utils.checkPhone(contactInfo))
          this.setData({
            errorMessage: '请输入正确的手机号或邮箱',
            flag: true
          })
        } else {
          this.setData({
            errorMessage: '',
            contactInfo: e.detail.value,
            flag: false
          })
        }
      }
    }else{
      this.setData({
        errorMessage: '请输入手机号或邮箱',
        contactInfo: '',
        flag: true
      })
    }
   
  },
  submit() {
    let that = this;
    let isLogin = wx.getStorageSync('isLogin')
    if (isLogin){
      if (!that.data.subAgain) {
        return
      }
      if (that.data.description === '') {
        Toast('请输入您需要描述的问题')
        return
      } else if (that.data.contactInfo === '') {
        Toast('请输入您的联系方式')
        return
      }
      let imgs = that.data.picJson;
      let flag = that.data.flag;
      let arr = [];
      let imgurl = [];
      if (!flag) {
        wx.showLoading()
        if (imgs.length > 0) {
          //上传图片
          for (let i = 0; i < imgs.length; i++) {
            arr.push(upload(imgs[i], "pic_root"))
          }
          Promise.all(arr).then(res => {
            imgurl = res.map(item => {
              return item.url
            })
            console.log(imgurl)
            this.submits(imgurl)
          })
        } else {
          this.submits(imgurl)
        }
      } else {
        wx.showToast({
          title: '您的输入有误，请检查输入内容',
          icon: 'none'
        })
        return
      }
    }else{
      wx.showModal({
        title: '系统提示',
        content: '您还未登录,是否去登录',
        success(res) {
          if (res.confirm) {
            wx.navigateTo({
              url: '/pages/login/login'
            })
          } else if (res.cancel) {
            that.setData({
              isLogin: false
            })
          }
        }
      })
    }
    
  },
  submits(imgurl){
    let that = this;
    //可以提交
    let obj = {
      contactInfo: that.data.contactInfo,//联系电话
      description: that.data.description,//描述内容
      type: that.data.type,//类型
      picJson: imgurl.length > 1 ? imgurl : imgurl.length > 0 ? imgurl[0] : ''//图片
    }
    utils.HttpRequst(true, '/baby-cloud-base/mobileFeedback/addItem', 'post', obj, res => {
      console.log(res)
      if(res.data.code == 200){
        Toast('保存成功')
        that.setData({
          isSave:true,
          subAgain:true
        })
        wx.navigateBack()
      }
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      contactInfo: wx.getStorageSync('telNum')
    })
  },
  onUnload: function () {
    let that = this;
    let contactInfo = that.data.contactInfo;
    let description = that.data.description;
    let isSave = that.data.isSave;
    if (contactInfo && description != '' && !isSave){
      wx.showModal({
        title: '系统提示',
        content: '您反馈的问题还未提交，请问是否要提交？',
        success(res) {
          if (res.confirm) {
            that.submit()
          } else if (res.cancel) {
            console.log('用户点击取消')
          }
        }
      })
    }
    // 页面销毁时执行
  },
})